// pages/shop/shop.js
const app = getApp()
const api = require("../../utils/net.js")
const util = require("../../utils/util.js")
const wxUtils = require("../../utils/wxUtils.js")
const {
  $Toast
} = require('../../dist/base/index')
const session = require("../../utils/session.js")
Page({
  /**
   * 页面的初始数据  
   */
  data: {

    tabIndicatorAnimation: {},
    isHideLoadMore: true,
    currentTab: 0,
    categoryId: 0,
    isLoading: true,
    limited: 10,
    offset: 0,
    loaded: false,
    discoveryCategorys: {},
    discoverys: {},
    tabIndicatorOffset: {},
    width: 0,
    xToast: {
      show: false
    },
  },
  showXToast: function(toast) {
    toast.show = true
    this.setData({
      xToast: toast
    })
    let that = this
    setTimeout(() => {
      that.setData({
        ['xToast.show']: false
      })
      if (toast.callBack) {
        toast.callBack()
      }
    }, 2000)
  },
  handleTabChange: function(e) {
    let lastTab = this.data.currentTab
    let targetTab = e.currentTarget.dataset.current
    let categoryId = e.currentTarget.dataset.id
    this.setData({
      currentTab: targetTab,
      categoryId: categoryId
    });

    let tabWidth = app.globalData.windowWidth / this.data.discoveryCategorys.length;
    let offsetTo = tabWidth * (targetTab)
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
      transformOrigin: "50% 50%",
      delay: 0,
    })


    animation.translateX(offsetTo).step()
    this.setData({
      tabIndicatorAnimation: animation.export()
    })


    this.showLoading()
    this.loadDataByCategory(true)


  },
  hideLoading: function() {
    if (this.data.isLoading)
      this.setData({
        isLoading: false
      })
  },
  showLoading: function() {
    if (!this.data.isLoading)
      this.setData({
        isLoading: true
      })
  },


  loadCategory: function(refresh) {
    api.get('discovery/discoveryCategory', {}, {
      onNext: res => {
        var width = 100 / res.length + "%";
        this.setData({
          discoveryCategorys: res,
          width: width
        })


      },
      onCompleted: () => this.hideLoading()
    })

  },

  loadDataByCategory: function(refresh) {
    var that = this
    var offset = this.data.offset

    if (refresh) {
      this.setData({
        offset: 0
      })
    } else {
      this.setData({
        offset: offset + this.data.limited
      })
    }


    api.get(`discovery/all`, {
      limit: 10,
      offset: this.data.offset,
      categoryId: that.data.categoryId
    }, {
      onNext: res => {
        let count = res.count
        let len = res.rows.length
        let loaded = len < 10
        offset += len
        if (res.rows) res.rows.forEach(it => it.friendlyTime = util.friendlyTime(it.createTime))
        let _visitors = refresh ? res.rows : this.data.discoverys.concat(res.rows)

        this.setData({
          discoverys: _visitors,
          dataLoaded: loaded
        })

        if (refresh) {
          //    this.floatTopAction()
          wx.stopPullDownRefresh()
        }
        this.setData({
          isHideLoadMore: res.rows == null || res.rows.length == 0,
          loaded: loaded
        })
      },
      onCompleted: () => this.hideLoading()
    })


  },

  stopRefresh: function() {
    wx.stopPullDownRefresh()
  },
  
  todetail: function(e) {
    var id = e.currentTarget.dataset.id
    util.toPage("../discoveryDetail/discoveryDetail", {
      "discoveryId": id
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {



    let that = this
    wx.getSystemInfo({
      success: (data) => {
        this.setData({
          screenWidth: data.screenWidth,
          screenHeight: data.screenHeight
        })
      }
    })
    this.loadCategory();


    session.check((msg) => {
      if (msg) {
        this.showXToast({
          title: msg,
          icon: 'no'
        })
        return
      }
    })
    this.loadDataByCategory(true)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

 
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.loadGoods(true)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

    this.loadDataByCategory(false)
  }
})